package model

import (
	"center-data-platform/app/content/rpc/content"
	"gorm.io/gorm"
	"time"
)

type Content struct {
	Id          int64      `gorm:"column:id;primaryKey" json:"id"`
	UserId      int64      `gorm:"column:user_id" json:"userId"`
	Type        int64      `gorm:"column:type" json:"type"`
	Title       string     `gorm:"column:title" json:"title"`
	Content     string     `gorm:"column:content" json:"content"`  // 内容
	Cover       string     `gorm:"column:cover" json:"cover"`      // 封面
	FileIds     string     `gorm:"column:file_ids" json:"fileIds"` // 上传阿里云盘的文件id
	Description string     `gorm:"column:description" json:"description"`
	Tags        string     `gorm:"column:tags" json:"tags"`
	CreatedTime time.Time  `gorm:"column:created_time" json:"createdTime"`
	UpdatedTime time.Time  `gorm:"column:updated_time" json:"updatedTime"`
	DeletedTime *time.Time `gorm:"column:deleted_time" json:"deletedTime"`
	Status      int64      `gorm:"column:status" json:"status"`
}

func (c *Content) List(db *gorm.DB, param *content.PostListReq) ([]Content, error) {
	var contents []Content
	var args []interface{}
	page, pageSize := 1, 10
	query := " true"
	if param.Title != "" {
		query += " and `title` like ?"
		args = append(args, "%"+param.Title+"%")
	}
	if len(param.Ids) > 0 {
		query += " and `id` in ?"
		args = append(args, param.Ids)
	}
	if len(param.UserIds) > 0 {
		query += " and `user_id` in ?"
		args = append(args, param.UserIds)
	}
	if len(param.Status) > 0 {
		query += " and `status` in ?"
		args = append(args, param.Status)
	}

	if param.PageSize > 0 {
		pageSize = int(param.PageSize)
	}
	if param.Page > 0 {
		page = int(param.Page)
	}
	err := db.Model(c).Where(query, args...).Offset((page - 1) * pageSize).Limit(pageSize).Find(&contents).Error
	if err != nil {
		return nil, err
	}
	return contents, nil
}
